package com.lgqz.himalayafm.fragment;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.lgqz.himalayafm.DrawListener;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.adapter.RadioProvinceAdapter;
import com.lgqz.himalayafm.bean.RadioProvinceEntity;
import com.lgqz.himalayafm.http.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class RadioFragmentProvince extends Fragment implements View.OnClickListener, Callback<RadioProvinceEntity> {
    private DrawListener listener;

    private View view;
    private Button mProStationBack;
    private TabLayout mProvinceTab;
    private List<RadioProvinceEntity.ProvinceEntity> mProvinceTabDatas;
    private RadioProvinceEntity radioProvinceEntity;
    private ViewPager mProViewpage;
    private RadioProvinceAdapter adapter;

    public RadioFragmentProvince() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_radios_province, container, false);
        listener = (DrawListener) getActivity();
        //获得界面上的实例
        initView();
        //给空间准备数据源
        initDatd();
        //给控件上的实例添加监听
        initListener();


        return view;
    }

    /**
     * 界面数据源的准备
     */
    private void initDatd() {

        //下载tablayout数据
        downloadTabData();

    }

    /**
     * 下载Tab的数据
     */
    private void downloadTabData() {

        HttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getProStaData().enqueue(this);
    }

    /**
     * 所有的控件添加监听器
     */
    private void initListener() {
        mProStationBack.setOnClickListener(this);
    }

    /**
     * 获得界面上的实例
     */
    private void initView() {
        //tab的数据源准备
        mProvinceTabDatas = new ArrayList<>();
        //返回按钮的实例
        mProStationBack = ((Button) view.findViewById(R.id.province_station_back));
        mProvinceTab = ((TabLayout) view.findViewById(R.id.radio_province_tablout));
        mProViewpage = ((ViewPager) view.findViewById(R.id.Radio_province_viewpage));
        //适配器的准备
        adapter = new RadioProvinceAdapter(getChildFragmentManager(), mProvinceTabDatas);
        mProViewpage.setAdapter(adapter);
    }

    /**
     * 点击按钮就返回界面
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.province_station_back:
                listener.closeDraw();

        }
    }

    /**
     * 下载数据
     *
     * @param call
     * @param response
     */
    @Override
    public void onResponse(Call<RadioProvinceEntity> call, Response<RadioProvinceEntity> response) {
        radioProvinceEntity = response.body();
        mProvinceTabDatas.addAll(radioProvinceEntity.getResult());
        adapter.notifyDataSetChanged();
        mProvinceTab.setupWithViewPager(mProViewpage);
    }

    @Override
    public void onFailure(Call<RadioProvinceEntity> call, Throwable t) {

    }
}